package com.pet.controller;

import com.pet.pojo.AdoptionApplication;
import com.pet.pojo.PageRequest;
import com.pet.pojo.PageResult;
import com.pet.pojo.Result;
import com.pet.service.AdoptionApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 领养申请控制器
 */
@RestController
@RequestMapping("/api/adoption")
public class AdoptionApplicationController {
    
    @Autowired
    private AdoptionApplicationService applicationService;
    
    /**
     * 提交领养申请
     * @param application 领养申请信息
     * @return 操作结果
     */
    @PostMapping("/apply")
    public Result<Void> apply(@RequestBody AdoptionApplication application) {
        boolean success = applicationService.submitApplication(application);
        if (success) {
            return Result.success("申请提交成功，请等待审核", null);
        } else {
            return Result.error("申请提交失败");
        }
    }
    
    /**
     * 更新申请状态
     * @param id 申请ID
     * @param status 申请状态
     * @return 操作结果
     */
    @PutMapping("/{id}/status/{status}")
    public Result<Void> updateStatus(@PathVariable Integer id, @PathVariable Integer status) {
        if (status < 0 || status > 2) {
            return Result.error("无效的状态值");
        }
        
        boolean success = applicationService.updateApplicationStatus(id, status);
        if (success) {
            return Result.success("状态更新成功", null);
        } else {
            return Result.error("状态更新失败");
        }
    }
    
    /**
     * 根据ID查询申请
     * @param id 申请ID
     * @return 申请信息
     */
    @GetMapping("/{id}")
    public Result<AdoptionApplication> getById(@PathVariable Integer id) {
        AdoptionApplication application = applicationService.getApplicationById(id);
        if (application == null) {
            return Result.error("申请不存在");
        }
        return Result.success(application);
    }
    
    /**
     * 根据宠物ID查询申请列表
     * @param petId 宠物ID
     * @param pageRequest 分页参数
     * @return 分页结果
     */
    @PostMapping("/pet/{petId}")
    public Result<PageResult<AdoptionApplication>> getByPetId(@PathVariable Integer petId, @RequestBody PageRequest pageRequest) {
        PageResult<AdoptionApplication> pageResult = applicationService.getApplicationsByPetId(petId, pageRequest);
        return Result.success(pageResult);
    }
    
    /**
     * 查询所有申请
     * @param pageRequest 分页参数
     * @return 分页结果
     */
    @PostMapping("/list")
    public Result<PageResult<AdoptionApplication>> list(@RequestBody PageRequest pageRequest) {
        PageResult<AdoptionApplication> pageResult = applicationService.getAllApplications(pageRequest);
        return Result.success(pageResult);
    }
} 